package com.wust.controller;

import com.wust.common.Const;
import com.wust.common.ServerResponse;
import com.wust.entity.User;
import com.wust.mwgate.SDKDemo;
import com.wust.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.http.HttpSession;

/**
 * Created by lenovo on 2018/5/16.
 */
@RestController
@RequestMapping("/api/user")
@EnableSwagger2
@Api(value = "user", description = "用户接口")
public class UserController {

    @Autowired
    private IUserService iUserService;


    /**
     * 登录接口
     * @param username
     * @param password
     * @param session
     * @return
     */
    //todo 横向越权与纵向越权
    @PostMapping("/dologin")
    @ApiOperation("用户登录")
    public ServerResponse<User> login(String username, String password , HttpSession session){
        ServerResponse<User> response = iUserService.login(username, password);
        if(response.isSuccess()){
            //将当前user放入session中
            session.setAttribute(Const.CURRENT_USER,response.getData());
        }
        return response;
    }

    @PostMapping("/dologout")
    @ApiOperation("用户退出登录")
    public ServerResponse<String> logout(HttpSession session){
        //将user从session中移除掉
        session.removeAttribute(Const.CURRENT_USER);
        return ServerResponse.createBySuccess();
    }

    @PostMapping("/doregister")
    @ApiOperation("注册新用户")
    public ServerResponse<String> register(User user){
        return iUserService.register(user);
    }

    @PostMapping("/docheckPhoneNumber")
    @ApiOperation("手机号验证")
    public ServerResponse<Integer> checkPhoneNumber(String telephone){
        return iUserService.checkPhoneNumber(telephone);
    }

    @PostMapping("/dogetInfo")
    @ApiOperation("获取用户全部信息")
    public ServerResponse<User> getUserInfo(String username){
        return iUserService.getUserInfo(username);
    }

    @PostMapping("/dobindDevice")
    @ApiOperation("用户绑定设备")
    public ServerResponse<String> bindDevice(String userId,String matchNumber, String matchCode){
        return iUserService.bindDevice(Integer.valueOf(userId),matchNumber,matchCode);
    }


}
